package com.compomics.prioritizer.db.accessors;

import java.util.HashMap;
import java.util.Vector;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * Created by IntelliJ IDEA.
 * Taxonomy: Niklaas Colaert
 * Date: 7-nov-2008
 * Time: 13:50:59
 * To change this template use File | Settings | File Templates.
 */
public class Taxonomy extends TaxonomyTableAccessor {

    /**
     * Simple wrapper for the superclass constructor.
     *
     * @param aParams   HashMap with the parameters.
     */
    public Taxonomy(HashMap aParams) {
        super(aParams);
    }

    /**
     * This constructor sets the only 'settable' field: taxonomyname.
     *
     * @param aName String with the full name for the taxonomy.
     */
    public Taxonomy(String aName) {
        super.setName(aName);
    }

    /**
     * This constructor reads a Taxonomy from a ResultSet. The ResultSet should be positioned such that
     * a single row can be read directly (i.e., without calling the 'next()' method on the ResultSet). <br />
     * The columns should be in this order: <br />
     * Column 1: taxonomy ID <br />
     * Column 2: name <br />
     * Column 3: accession
     * Column 4: username <br />
     * Column 5: creationdate <br />
     * Column 6: modificationdate.
     *
     * @param   aRS ResultSet to read the data from.
     * @exception   java.sql.SQLException    when reading the ResultSet failed.
     */
    public Taxonomy(ResultSet aRS) throws SQLException {
        this.iTaxonomyid = aRS.getLong(1);
        this.iName = aRS.getString(2);
        this.iAccession = aRS.getLong(3);
        iUsername = aRS.getString(4);
        iCreationdate = (java.sql.Timestamp)aRS.getObject(5);
        iModificationdate = (java.sql.Timestamp)aRS.getObject(6);
    }

    /**
     * This method retrieves all taxonomys from the connection and stores them in a HashMap. <br />
     * The taxonomyid is the key (Long type) and the Taxonomy object is the value.
     *
     * @param aConn Connection to retrieve the taxonomys from.
     * @return  HashMap with the taxonomys, taxonomyid is the key (Long type) and Taxonomy objects are the values.
     * @throws SQLException when the retrieve failed.
     */
    public static HashMap getAllTaxonomysAsMap(Connection aConn) throws SQLException {
        HashMap lTaxonomys = new HashMap();
        PreparedStatement prep = aConn.prepareStatement("select * from taxonomy");
        ResultSet rs = prep.executeQuery();
        while(rs.next()) {
            Taxonomy temp = new Taxonomy(rs);
            lTaxonomys.put(new Long(temp.getTaxonomyid()),temp);
        }
        rs.close();
        prep.close();

        return lTaxonomys;
    }

    /**
     * This method retrieves all taxonomys from the connection and stores them in a Taxonomy[].
     *
     * @param aConn Connection to retrieve the taxonomys from.
     * @return  Taxonomy[] with the taxonomys.
     * @throws SQLException when the retrieve failed.
     */
    public static Taxonomy[] getAllTaxonomys(Connection aConn) throws SQLException {
        PreparedStatement prep = aConn.prepareStatement("select * from taxonomy");
        ResultSet rs = prep.executeQuery();
        Vector temp = new Vector();
        while(rs.next()) {
            temp.add(new Taxonomy(rs));
        }
        Taxonomy[] result = new Taxonomy[temp.size()];
        temp.toArray(result);
        rs.close();
        prep.close();
        return result;
    }

    /**
     * This methods reads all taxonomys from the Taxonomy table that have a
     * calibrated differential standard deviation.
     *
     * @param aConn Connection to read the taxonomys from.
     * @param aId Connection to read the taxonomys from.
     * @return  Taxonomy with the taxonomy in the 'Taxonomy' table.
     * @throws SQLException when the retrieving of the taxonomy went wrong.
     */
    public static Taxonomy getTaxonomyById(Connection aConn, Long aId) throws SQLException {
        PreparedStatement prep = aConn.prepareStatement("select * from taxonomy where taxonomyid = ?");
        prep.setLong(1, aId);
        ResultSet rs = prep.executeQuery();
        Taxonomy lTaxonomy = null;
        while(rs.next()) {
            lTaxonomy = new Taxonomy(rs);
        }
        rs.close();
        prep.close();

        return lTaxonomy;
    }


    /**
     * This method returns a String representation of the Taxonomy, ie.: the name.
     *
     * @return  String  with the name of the taxonomy.
     */
    public String toString() {
        return this.iName;
    }
}

